<?php

class Project extends CI_Controller {
 
    public function __construct()
    {
        parent::__construct();
        $this->load->model('project_model');
    }
    
    public  function index(){
       if($this->session->userdata('is_logged_in')){
            $this->list_project();
        }else{
            $this->load->view('admin/login');   
        }
 
    }
    public function  list_project(){
        $return_value = $this->project_model->get_project();
        $data['project'] =$return_value['result'];
        $data['message'] =$return_value['message'];
        $data['state'] =$return_value['state'];
        $data['main_content'] = 'admin/project/list';
        $this->load->view('includes/template', $data); 
    }
    
    public function add()
    {
        $this->load->model('department_model');
            date_default_timezone_set('America/Los_Angeles');
        if ($this->input->server('REQUEST_METHOD') === 'POST')
        {
            //form validation
            $this->form_validation->set_rules('pro_id', 'Project ID', 'required');
            $this->form_validation->set_rules('name', 'Name', 'required');
            $this->form_validation->set_rules('hireday', 'Hireday', 'required');
            $this->form_validation->set_rules('description', 'Description', 'required');
            $this->form_validation->set_rules('bucket', 'Bucket', 'required');
            $this->form_validation->set_rules('mgr_id', 'Manager Id', 'required');
            $this->form_validation->set_rules('est_endday', 'Estimate Endday', 'required');
            $this->form_validation->set_rules('end_day', 'End Day', 'required');
            $this->form_validation->set_error_delimiters('<div class="alert alert-error"><a class="close" data-dismiss="alert">×</a><strong>', '</strong></div>');

            //if the form has passed through the validation
            if ($this->form_validation->run())
            {
                  $data_to_store = array(
                    'pro_id' => $this->input->post('pro_id'),
                    'name' => $this->input->post('name'),
                    'hireday' =>date('d-M-y',strtotime( $this->input->post('hireday'))),
                    'description' => $this->input->post('description'),          
                    'bucket' => $this->input->post('bucket'),
                    'mgr_id' => $this->input->post('mgr_id'),
                    'est_endday' =>date('d-M-y',strtotime( $this->input->post('est_endday'))),
                    'end_day' =>date('d-M-y',strtotime( $this->input->post('end_day')))
                );

//                    $data = $data_to_store;
//    $sql = "INSERT INTO ".$this->config->item('schema').".project(pro_id,name,hireday,description,bucket,mgr_id,est_endday,end_day)"
//                . "VALUES ('".$data['pro_id']."','".$data['name']."','".$data['hireday']."','".$data['description']."','".$data['bucket']."'"
//                . ",'".$data['mgr_id']."','".$data['est_endday']."','".$data['end_day']."')" ; 
//                
//      
//                 $data['info'] =$sql;
//            $data['main_content'] = 'test';
//        $this->load->view('includes/template', $data); 

               $return_value = $this->project_model->store_project($data_to_store);
               $data['department'] =$return_value['result'];
               $data['message'] =$return_value['message'];
               $data['state'] =$return_value['state'];
               
               if($return_value['state']== 1){
                    $this->session->set_flashdata('flash_message', 'added');
                    redirect('index.php/project');
               } else {
                    $this->session->set_flashdata('flash_message', 'not_added');

                    redirect('index.php/project/add');
                    // redirect('index.php/project/');
               }

            }
        }
        //fetch manufactures data to populate the select field
      //  $data['manufactures'] = $this->manufacturers_model->get_manufacturers();
        //load the view
         $return_value = $this->department_model->get_department();
         $data['department'] =$return_value['result'];
         $data['message'] =$return_value['message'];
         $data['state'] =$return_value['state'];
         $data['main_content'] = 'admin/project/add';
         $this->load->view('includes/template', $data); 

        
    }  //End # Add

    public function update()
    {
           $this->load->model('department_model');
           date_default_timezone_set('America/Los_Angeles');
        //product id 
        $id = $this->uri->segment(3);
        //if save button was clicked, get the data sent via post
        if ($this->input->server('REQUEST_METHOD') === 'POST')
        {
            //form validation
           //form validation
            $this->form_validation->set_rules('pro_id', 'Project ID', 'required');
            $this->form_validation->set_rules('name', 'Name', 'required');
            $this->form_validation->set_rules('hireday', 'Hireday', 'required');
            $this->form_validation->set_rules('description', 'Description', 'required');
            $this->form_validation->set_rules('bucket', 'Bucket', 'required');
            $this->form_validation->set_rules('mgr_id', 'Manager Id', 'required');
            $this->form_validation->set_rules('est_endday', 'Estimate Endday', 'required');
            $this->form_validation->set_rules('end_day', 'End Day', 'required');
            $this->form_validation->set_error_delimiters('<div class="alert alert-error"><a class="close" data-dismiss="alert">×</a><strong>', '</strong></div>');
            //if the form has passed through the validation
            if ($this->form_validation->run())
            {
    
                $data_to_store = array(
                    'pro_id' => $this->input->post('pro_id'),
                    'name' => $this->input->post('name'),
                    'hireday' =>date('d-M-y',strtotime( $this->input->post('hireday'))),
                    'description' => $this->input->post('description'),          
                    'bucket' => $this->input->post('bucket'),
                    'mgr_id' => $this->input->post('mgr_id'),
                    'est_endday' =>date('d-M-y',strtotime( $this->input->post('est_endday'))),
                    'end_day' =>date('d-M-y',strtotime( $this->input->post('end_day')))
                );
                //if the insert has returned true then we show the flash message
               
                
                                                $data = $data_to_store;
                                
//         $sql = "UPDATE Project
//                    SET 
//                         name='".$data['name']."'"
//                         . ",hireday='".$data['hireday']."'"
//                         . ",description='".$data['description']."'"
//                         . ",bucket='".$data['bucket']."'"
//                         . ",mgr_id='".$data['mgr_id']."'"
//                         . ",est_endday='".$data['est_endday']."'"
//                         . ",end_day='".$data['end_day']."'"
//                  . "WHERE pro_id='".$data['pro_id']."'";
//                
//      
//                 $data['info'] =$sql;
//            $data['main_content'] = 'test';
//        $this->load->view('includes/template', $data); 
                
               $return_value = $this->project_model->update_project($data_to_store);             
               $data['department'] =$return_value['result'];
               $data['message'] =$return_value['message'];
               $data['state'] =$return_value['state'];

               if($return_value['state']== 1){
                     $this->session->set_flashdata('flash_message', 'updated');
                    redirect('index.php/project');
               } else {
                     $this->session->set_flashdata('flash_message', 'not_updated');
                    redirect('index.php/project/update/'.$data_to_store['pro_id'].'');
              }
//               
               


            }//validation run

        }

        //projec data
         $return_value = $this->project_model->get_project_by_id($id);
         $array_project =$return_value['result'];
         $data['project'] = $array_project[0];
         $data['message'] =$return_value['message'];
         $data['state'] =$return_value['state'];
    
         $return_value1 = $this->department_model->get_department();
         $data['department'] =$return_value1['result'];
        //load the view
        $data['main_content'] = 'admin/project/edit';
        $this->load->view('includes/template', $data);            

    }//update
    
     /**
    * Delete product by his id
    * @return void
    */
    public function delete()
    {
          $id = $this->uri->segment(3);
          $return_value = $this->project_model->delete_project($id);
               $data['department'] =$return_value['result'];
               $data['message'] =$return_value['message'];
               $data['state'] =$return_value['state'];

               if($return_value['state']== 1){
                     $this->session->set_flashdata('flash_message', 'deleted');
                    redirect('index.php/project');
               } else {
                     $this->session->set_flashdata('flash_message', 'not_deleted');
                    redirect('index.php/project');
               }
    }//edit
    
     public function update_a_field()
    {
            $this->form_validation->set_rules('af_pro_field', 'Project Field', 'required');
            $this->form_validation->set_rules('af_value', 'Value', 'required');
            $this->form_validation->set_rules('af_pro_id', 'Project ID', 'required');
            $this->form_validation->set_error_delimiters('<div class="alert alert-error"><a class="close" data-dismiss="alert">×</a><strong>', '</strong></div>');
            //if the form has passed through the validation
            if ($this->form_validation->run())
            {
    
                $data_to_store = array(
                    'af_pro_field' => $this->input->post('af_pro_field'),
                    'af_value' => $this->input->post('af_value'),
                    'af_pro_id' => $this->input->post('af_pro_id')
                );
                //if the insert has returned true then we show the flash message
               
             
                
//                              $data = $data_to_store;
//             $sql = "UPDATE ".$this->config->item('schema').".project
//                    SET 
//                      ".$data['af_pro_field']."=".$data['af_value']." "
//                       . "WHERE pro_id='".$data['af_pro_id']."'";
//             $data['info'] =$sql;    
//            $data['main_content'] = 'test';
//        $this->load->view('includes/template', $data); 
                              
              $return_value = $this->project_model->update_af_in_project($data_to_store);
                
               $data['message'] =$return_value['message'];
               $data['state'] =$return_value['state'];
               if($return_value['state']== 1){
                     $this->session->set_flashdata('flash_message', 'updated');
                    redirect('index.php/project');
               } else {
                     $this->session->set_flashdata('flash_message', 'not_updated');
                     redirect('index.php/project');
               }
            }
            else {
              $this->list_project();
            }
        }
}

